package com.example.yunifang.fragment;

import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.drawable.Drawable;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.RadioButton;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.yunifang.QuanBuActivity;
import com.example.yunifang.R;
import com.example.yunifang.XiangQingActivity;
import com.example.yunifang.ZhuWebActivity;
import com.example.yunifang.adapter.Fragment1MyPageAdapter;
import com.example.yunifang.adapter.MyGridView1Adapter;
import com.example.yunifang.adapter.MyGridView2Adapter;
import com.example.yunifang.adapter.MyGridViewOutAdapter;
import com.example.yunifang.bean.HeaderViewPagerBean;
import com.example.yunifang.bean.HeaderViewPagerBean.Data.Ad1;
import com.example.yunifang.bean.HeaderViewPagerBean.Data.Ad3;
import com.example.yunifang.bean.HeaderViewPagerBean.Data.Ad4;
import com.example.yunifang.bean.HeaderViewPagerBean.Data.Ad5;
import com.example.yunifang.bean.HeaderViewPagerBean.Data.GroupBriefs;
import com.example.yunifang.bean.HeaderViewPagerBean.Data.GroupBriefs.BirefList1;
import com.example.yunifang.myview.MyZhuGridView;
import com.example.yunifang.util.DateUtilsl;
import com.google.gson.Gson;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.nostra13.universalimageloader.core.ImageLoader;

public class Fragment1 extends Fragment implements OnItemClickListener {
	private Handler handler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			int count = msg.arg1;
			fragment_start_my_vp.setCurrentItem(count);
		};
	};
	private Context context;
	private RelativeLayout fragment_text_tv1;
	private RelativeLayout layout;
	private ViewPager fragment_start_my_vp;
	private ArrayList<Ad1> data = new ArrayList<Ad1>();
	private ArrayList<Ad3> data3 = new ArrayList<Ad3>();
	private ArrayList<Ad4> data4 = new ArrayList<Ad4>();
	private ArrayList<Ad5> data5 = new ArrayList<Ad5>();
	private ArrayList<GroupBriefs> groupdata = new ArrayList<GroupBriefs>();
	private ArrayList<BirefList1> birefList = new ArrayList<BirefList1>();
	private ArrayList<BirefList1> birefList2 = new ArrayList<BirefList1>();
	private ArrayList<BirefList1> birefList3 = new ArrayList<BirefList1>();
	private ArrayList<BirefList1> birefList4 = new ArrayList<BirefList1>();
	private ArrayList<BirefList1> birefList5 = new ArrayList<BirefList1>();
	private ArrayList<BirefList1> birefList6 = new ArrayList<BirefList1>();
	private ArrayList<BirefList1> birefList7 = new ArrayList<BirefList1>();
	private LinearLayout home_ll_dot;
	private ArrayList<ImageView> imageViewslist;
	private Timer timer;
	private GridView gd;
	private ImageView fragment1_out_gd_img;
	private TextView fragment1_out_gd_tv;
	private ImageView fragment1_out_gd_img1;
	private ImageView fragment1_out_gd_img2;
	private ImageView fragment_zhu_gd1_img1;
	private MyZhuGridView fragment_zhu_gd1_gd1;
	private ImageView fragment_zhu_gd2_img1;
	private MyZhuGridView fragment_zhu_gd2_gd1;
	private ImageView fragment_zhu_gd3_img1;
	private MyZhuGridView fragment_zhu_gd3_gd1;
	private ImageView fragment_zhu_gd4_img1;
	private MyZhuGridView fragment_zhu_gd4_gd1;
	private ImageView fragment_zhu_gd5_img1;
	private MyZhuGridView fragment_zhu_gd5_gd1;
	private ImageView fragment_zhu_gd6_img1;
	private MyZhuGridView fragment_zhu_gd6_gd1;
	private ImageView fragment_zhu_gd7_img1;
	private MyZhuGridView fragment_zhu_gd7_gd1;
	private ScrollView ssss;
	private TextView fragment1_bt_more;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View view = View.inflate(getActivity(), R.layout.fragment1, null);
		layout = (RelativeLayout) view.findViewById(R.id.include_no_wifi);
		gd = (GridView) view.findViewById(R.id.fragment1_out_gd);
		fragment_text_tv1 = (RelativeLayout) view
				.findViewById(R.id.fragment_text_tv1);
		home_ll_dot = (LinearLayout) view.findViewById(R.id.home_ll_dot);
		context = getActivity();
		RadioButton button = (RadioButton) view.findViewById(R.id.no_wifi_btn);
		getWe();
		button.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				getWe();
			}
		});
		ssss = (ScrollView) view.findViewById(R.id.ssss);
		// ssss.smoothScrollTo(0, 0);
		fragment_start_my_vp = (ViewPager) view
				.findViewById(R.id.fragment_start_my_vp);
		fragment_start_my_vp
				.setOnPageChangeListener(new OnPageChangeListener() {

					@Override
					public void onPageSelected(int arg0) {
						// TODO Auto-generated method stub
						for (int i = 0; i < data.size(); i++) {
							if (i == arg0 % data.size()) {
								imageViewslist.get(i).setImageDrawable(
										getResources().getDrawable(
												R.drawable.dots_nomal));
							} else {
								imageViewslist.get(i).setImageDrawable(
										getResources().getDrawable(
												R.drawable.dots_focuse));
							}
						}

					}

					@Override
					public void onPageScrolled(int arg0, float arg1, int arg2) {
						// TODO Auto-generated method stub

					}

					@Override
					public void onPageScrollStateChanged(int arg0) {
						// TODO Auto-generated method stub

					}
				});
		fragment1_out_gd_img = (ImageView) view
				.findViewById(R.id.fragment1_out_gd_img);
		fragment1_out_gd_tv = (TextView) view
				.findViewById(R.id.fragment1_out_gd_tv);
		fragment1_out_gd_img1 = (ImageView) view
				.findViewById(R.id.fragment1_out_gd_img1);
		fragment1_out_gd_img2 = (ImageView) view
				.findViewById(R.id.fragment1_out_gd_img2);
		fragment_zhu_gd1_img1 = (ImageView) view
				.findViewById(R.id.fragment_zhu_gd1_img1);
		fragment_zhu_gd1_gd1 = (MyZhuGridView) view
				.findViewById(R.id.fragment_zhu_gd1_gd1);
		fragment_zhu_gd2_img1 = (ImageView) view
				.findViewById(R.id.fragment_zhu_gd2_img1);
		fragment_zhu_gd2_gd1 = (MyZhuGridView) view
				.findViewById(R.id.fragment_zhu_gd2_gd1);
		fragment_zhu_gd3_img1 = (ImageView) view
				.findViewById(R.id.fragment_zhu_gd3_img1);
		fragment_zhu_gd3_gd1 = (MyZhuGridView) view
				.findViewById(R.id.fragment_zhu_gd3_gd1);
		fragment_zhu_gd4_img1 = (ImageView) view
				.findViewById(R.id.fragment_zhu_gd4_img1);
		fragment_zhu_gd4_gd1 = (MyZhuGridView) view
				.findViewById(R.id.fragment_zhu_gd4_gd1);
		fragment_zhu_gd5_img1 = (ImageView) view
				.findViewById(R.id.fragment_zhu_gd5_img1);
		fragment_zhu_gd5_gd1 = (MyZhuGridView) view
				.findViewById(R.id.fragment_zhu_gd5_gd1);
		fragment_zhu_gd6_img1 = (ImageView) view
				.findViewById(R.id.fragment_zhu_gd6_img1);
		fragment_zhu_gd6_gd1 = (MyZhuGridView) view
				.findViewById(R.id.fragment_zhu_gd6_gd1);
		fragment_zhu_gd7_img1 = (ImageView) view
				.findViewById(R.id.fragment_zhu_gd7_img1);
		fragment_zhu_gd7_gd1 = (MyZhuGridView) view
				.findViewById(R.id.fragment_zhu_gd7_gd1);
		fragment1_bt_more = (TextView) view
				.findViewById(R.id.fragment1_bt_more);
		fragment1_bt_more.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(getActivity(), QuanBuActivity.class);
				startActivity(intent);
				((Activity) context).overridePendingTransition(
						R.anim.outactivity, R.anim.inactivity);
			}
		});
		return view;
	}

	public void getJson() {

		HttpUtils utils = new HttpUtils();
		utils.send(
				HttpMethod.GET,
				"http://m.yunifang.com/yunifang/mobile/home?random=59676&encode=62d458fefce9c740359873cc19b05188",
				new RequestCallBack<String>() {

					@Override
					public void onFailure(HttpException arg0, String arg1) {
						// TODO Auto-generated method stub
						SharedPreferences sp = getActivity()
								.getSharedPreferences("fragment",
										getActivity().MODE_PRIVATE);
						String json = sp.getString("fragment1_1", null);
						if (json != null) {
							Gson gson = new Gson();
							HeaderViewPagerBean bean = gson.fromJson(json,
									HeaderViewPagerBean.class);
							init(bean);
						}
					}

					@Override
					public void onSuccess(ResponseInfo<String> arg0) {
						// TODO Auto-generated method stub
						getGson(arg0);
						initDots();
						fragment_start_my_vp
								.setAdapter(new Fragment1MyPageAdapter(data,
										context));
						gd.setAdapter(new MyGridViewOutAdapter(data5, context));
						gd.setFocusable(false);
						if (timer == null) {
							timer = new Timer();
							timer.schedule(new TimerTask() {
								int count = 0;

								@Override
								public void run() {
									// TODO Auto-generated method stub
									Message msg = Message.obtain();
									msg.arg1 = count;
									handler.sendMessage(msg);
									count++;
								}
							}, 0, 2000);
						}
						System.out.println(data.size());
					}

				});

	}

	public void getGson(ResponseInfo<String> arg0) {
		Gson gson = new Gson();
		String json = arg0.result;
		SharedPreferences sp = getActivity().getSharedPreferences("fragment1",
				getActivity().MODE_PRIVATE);
		Editor editor = sp.edit();
		editor.putString("fragment1_1", json);
		editor.commit();
		System.out.println(json);
		HeaderViewPagerBean bean = gson.fromJson(json,
				HeaderViewPagerBean.class);
		init(bean);

	}

	public void init(final HeaderViewPagerBean bean) {
		data.clear();
		data.addAll(bean.data.ad1);
		data5.clear();
		data5.addAll(bean.data.ad5);
		data3.clear();
		data3.addAll(bean.data.ad3);
		data4.clear();
		data4.addAll(bean.data.ad4);
		groupdata.clear();
		groupdata.addAll(bean.data.groupBriefs);
		birefList.clear();
		birefList.addAll(bean.data.groupBriefs.get(0).briefList);
		birefList2.clear();
		birefList2.addAll(bean.data.groupBriefs.get(1).briefList);
		birefList3.clear();
		birefList3.addAll(bean.data.groupBriefs.get(2).briefList);
		birefList4.clear();
		birefList4.addAll(bean.data.groupBriefs.get(3).briefList);
		birefList5.clear();
		birefList5.addAll(bean.data.groupBriefs.get(4).briefList);
		birefList6.clear();
		birefList6.addAll(bean.data.groupBriefs.get(5).briefList);
		// birefList7.clear();
		// birefList7.addAll(bean.data.groupBriefs.get(6).briefList);
		ImageLoader.getInstance().displayImage(data3.get(0).lottery_img,
				fragment1_out_gd_img);
		fragment1_out_gd_tv.setText(data3.get(0).prize_name);
		ImageLoader.getInstance().displayImage(data4.get(0).image,
				fragment1_out_gd_img1);
		fragment1_out_gd_img1.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(context, ZhuWebActivity.class);
				String str = data4.get(0).ad_type_dynamic_data;
				intent.putExtra("str", str);
				context.startActivity(intent);
				((Activity) context).overridePendingTransition(
						R.anim.outactivity, R.anim.inactivity);
			}
		});
		ImageLoader.getInstance().displayImage(data4.get(1).image,
				fragment1_out_gd_img2);
		fragment1_out_gd_img2.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(context, ZhuWebActivity.class);
				String str = data4.get(1).ad_type_dynamic_data;
				intent.putExtra("str", str);
				context.startActivity(intent);
				((Activity) context).overridePendingTransition(
						R.anim.outactivity, R.anim.inactivity);
			}
		});
		ImageLoader.getInstance().displayImage(groupdata.get(0).image,
				fragment_zhu_gd1_img1);
		ImageLoader.getInstance().displayImage(groupdata.get(1).image,
				fragment_zhu_gd2_img1);
		ImageLoader.getInstance().displayImage(groupdata.get(2).image,
				fragment_zhu_gd3_img1);
		ImageLoader.getInstance().displayImage(groupdata.get(3).image,
				fragment_zhu_gd4_img1);
		ImageLoader.getInstance().displayImage(groupdata.get(4).image,
				fragment_zhu_gd5_img1);
		ImageLoader.getInstance().displayImage(groupdata.get(5).image,
				fragment_zhu_gd6_img1);
		// ImageLoader.getInstance().displayImage(groupdata.get(6).image,
		// fragment_zhu_gd7_img1);
		MyGridView1Adapter adapter1 = new MyGridView1Adapter(birefList, context);
		fragment_zhu_gd1_gd1.setAdapter(adapter1);
		fragment_zhu_gd1_gd1.setFocusable(false);
		MyGridView2Adapter adapter2 = new MyGridView2Adapter(birefList2,
				context);
		fragment_zhu_gd2_gd1.setAdapter(adapter2);
		fragment_zhu_gd2_gd1.setFocusable(false);
		MyGridView2Adapter adapter3 = new MyGridView2Adapter(birefList3,
				context);
		fragment_zhu_gd3_gd1.setAdapter(adapter3);
		fragment_zhu_gd3_gd1.setFocusable(false);
		MyGridView2Adapter adapter4 = new MyGridView2Adapter(birefList4,
				context);
		fragment_zhu_gd4_gd1.setAdapter(adapter4);
		fragment_zhu_gd4_gd1.setFocusable(false);
		MyGridView2Adapter adapter5 = new MyGridView2Adapter(birefList5,
				context);
		fragment_zhu_gd5_gd1.setAdapter(adapter5);
		fragment_zhu_gd5_gd1.setFocusable(false);
		MyGridView2Adapter adapter6 = new MyGridView2Adapter(birefList6,
				context);
		fragment_zhu_gd6_gd1.setAdapter(adapter6);
		fragment_zhu_gd6_gd1.setFocusable(false);
		MyGridView2Adapter adapter7 = new MyGridView2Adapter(birefList7,
				context);
		fragment_zhu_gd7_gd1.setAdapter(adapter7);
		fragment_zhu_gd7_gd1.setFocusable(false);
		fragment_zhu_gd1_gd1.setOnItemClickListener(this);
		fragment_zhu_gd2_gd1.setOnItemClickListener(this);
		fragment_zhu_gd3_gd1.setOnItemClickListener(this);
		fragment_zhu_gd4_gd1.setOnItemClickListener(this);
		fragment_zhu_gd5_gd1.setOnItemClickListener(this);
		fragment_zhu_gd6_gd1.setOnItemClickListener(this);
		fragment_zhu_gd7_gd1.setOnItemClickListener(this);
		gd.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				String str = bean.data.ad5.get(arg2).url;
				if (str != null) {
					Intent intent = new Intent(context, ZhuWebActivity.class);
					intent.putExtra("str", str);
					startActivity(intent);
					((Activity) context).overridePendingTransition(
							R.anim.outactivity, R.anim.inactivity);
				}
			}
		});

	}

	public void getWe() {
		// TODO Auto-generated method stub
		if (isNetworkAvailable(context)) {
			Toast.makeText(context.getApplicationContext(), "当前网络可用", 0).show();
			fragment_text_tv1.setVisibility(View.VISIBLE);
			layout.setVisibility(View.INVISIBLE);
			getJson();

		} else {
			Toast.makeText(context.getApplicationContext(), "当前网络不可以", 0)
					.show();
			fragment_text_tv1.setVisibility(View.INVISIBLE);
			layout.setVisibility(View.VISIBLE);
		}
	}

	private boolean isNetworkAvailable(Context zhuActivity) {
		// TODO Auto-generated method stub
		Context context = zhuActivity.getApplicationContext();
		// ��ȡ�ֻ��������ӹ�����󣨰�����wi-fi,net�����ӵĹ��?
		ConnectivityManager connectivityManager = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);

		if (connectivityManager == null) {
			return false;
		} else {
			// ��ȡNetworkInfo����
			NetworkInfo[] networkInfo = connectivityManager.getAllNetworkInfo();

			if (networkInfo != null && networkInfo.length > 0) {
				for (int i = 0; i < networkInfo.length; i++) {
					// System.out.println(i + "===״̬===" +
					// networkInfo[i].getState());
					// System.out.println(i + "===����===" +
					// networkInfo[i].getTypeName());
					// �жϵ�ǰ����״̬�Ƿ�Ϊ����״̬
					if (networkInfo[i].getState() == NetworkInfo.State.CONNECTED) {
						return true;
					}
				}
			}
		}
		return false;
	}

	private void initDots() {
		// TODO Auto-generated method stub
		imageViewslist = new ArrayList<ImageView>();
		imageViewslist.clear();
		home_ll_dot.removeAllViews();
		for (int i = 0; i < data.size(); i++) {
			ImageView imageView = new ImageView(getActivity());
			Drawable drawable = null;
			if (i == 0) {
				drawable = getResources().getDrawable(R.drawable.dots_nomal);
			} else {
				drawable = getResources().getDrawable(R.drawable.dots_focuse);
			}
			imageView.setImageDrawable(drawable);
			LayoutParams params = new LayoutParams(dip2px(getActivity(), 10),
					dip2px(getActivity(), 10));
			params.setMargins(dip2px(getActivity(), 10), 0,
					dip2px(getActivity(), 10), 0);
			home_ll_dot.addView(imageView, params);
			imageViewslist.add(imageView);
		}
		;

	}

	public int dip2px(FragmentActivity fragmentActivity, float dpvalue) {
		final float scale = fragmentActivity.getResources().getDisplayMetrics().density;
		return (int) (dpvalue * scale + 0.5f);
	}

	/*
	 * @Override public void onResume() { // TODO Auto-generated method stub
	 * ssss.smoothScrollTo(0, 0); super.onResume(); }
	 */

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		// TODO Auto-generated method stub
		switch (arg0.getId()) {
		case R.id.fragment_zhu_gd1_gd1:
			int id = groupdata.get(0).briefList.get(arg2).id;
			Intent intent = new Intent(context, XiangQingActivity.class);
			intent.putExtra("id", id);
			startActivity(intent);
			((Activity) context).overridePendingTransition(R.anim.outactivity,
					R.anim.inactivity);
			break;
		case R.id.fragment_zhu_gd2_gd1:
			int id2 = groupdata.get(1).briefList.get(arg2).id;
			Intent intent2 = new Intent(context, XiangQingActivity.class);
			intent2.putExtra("id", id2);
			startActivity(intent2);
			((Activity) context).overridePendingTransition(R.anim.outactivity,
					R.anim.inactivity);
			break;
		case R.id.fragment_zhu_gd3_gd1:
			int id3 = groupdata.get(2).briefList.get(arg2).id;
			Intent intent3 = new Intent(context, XiangQingActivity.class);
			intent3.putExtra("id", id3);
			startActivity(intent3);
			((Activity) context).overridePendingTransition(R.anim.outactivity,
					R.anim.inactivity);
			break;
		case R.id.fragment_zhu_gd4_gd1:
			int id4 = groupdata.get(3).briefList.get(arg2).id;
			Intent intent4 = new Intent(context, XiangQingActivity.class);
			intent4.putExtra("id", id4);
			startActivity(intent4);
			((Activity) context).overridePendingTransition(R.anim.outactivity,
					R.anim.inactivity);
			break;
		case R.id.fragment_zhu_gd5_gd1:
			int id5 = groupdata.get(4).briefList.get(arg2).id;
			Intent intent5 = new Intent(context, XiangQingActivity.class);
			intent5.putExtra("id", id5);
			startActivity(intent5);
			((Activity) context).overridePendingTransition(R.anim.outactivity,
					R.anim.inactivity);
			break;
		case R.id.fragment_zhu_gd6_gd1:
			int id6 = groupdata.get(5).briefList.get(arg2).id;
			Intent intent6 = new Intent(context, XiangQingActivity.class);
			intent6.putExtra("id", id6);
			startActivity(intent6);
			((Activity) context).overridePendingTransition(R.anim.outactivity,
					R.anim.inactivity);
			break;
		case R.id.fragment_zhu_gd7_gd1:
			int id7 = groupdata.get(6).briefList.get(arg2).id;
			Intent intent7 = new Intent(context, XiangQingActivity.class);
			intent7.putExtra("id", id7);
			startActivity(intent7);
			((Activity) context).overridePendingTransition(R.anim.outactivity,
					R.anim.inactivity);
			break;
		default:
			break;
		}
	}

	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		if (timer != null) {
			timer.cancel();
		}
	}

}
